package fge;

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.*;

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;

public class CodeEditor extends JDialog {

	private doubleClickable obj;
	private RSyntaxTextArea textArea;

	public CodeEditor(doubleClickable _obj) 
	{
		this.obj = _obj;

		this.textArea = new RSyntaxTextArea();
		this.textArea.setText(this.obj.getFunc());
		this.textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);

		RTextScrollPane sp = new RTextScrollPane(textArea);
		JPanel cp = new JPanel(new BorderLayout());
		cp.add(sp);

		this.add(cp);
		this.setTitle(this.obj + " - Function editor");
		this.addWindowListener(new WindowEventHandler());
		this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		this.setBounds(200, 200, 600, 400);
		this.setVisible(true);
	}

	class WindowEventHandler extends WindowAdapter {
		@Override
		public void windowClosing(WindowEvent evt) {
//			System.out.println("closing frame with text: " + CodeEditor.this.textArea.getText());
			CodeEditor.this.obj.setFunc(CodeEditor.this.textArea.getText());
			CodeEditor.this.obj.removeEditor();
		}
	}
}
